﻿using AuroraPlatform.Controls;
using AuroraPlatform.TCP;
using AuroraPlatform.Tools;
using AuroraPlatform.ViewModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using System.Windows.Shapes;

namespace AuroraPlatform.View.Level2
{
    public partial class LightingClassView : UserControl, IPageProvider
    {
        private NaviTo2Param _parameter;

        public LightingClassView()
        {
            InitializeComponent();

            if (BusinessContext.Singleton.ParamIterator.Current is NaviTo2Param)
                _parameter = (NaviTo2Param)BusinessContext.Singleton.ParamIterator.Current;

            if (_parameter != null)
            {
                if (_parameter.Parameter.HasClass)
                {
                    ResourceProvider rs = new ResourceProvider();
                    var item = (from r in rs.ProdTypes
                                where r.ItemID == _parameter.Parameter.Class
                                select r).FirstOrDefault();

                    if (item != null)
                        Title.Text = item.ItemName;
                }

                Loading();
            }
        }

        private LightingClassModel ConvertForm(ProdClass lighting)
        {
            LightingClassModel vm = new LightingClassModel();
            if (lighting.HasProdSeries)
                vm.ProdSeries = lighting.ProdSeries;

            if (lighting.HasProdLabel)
                vm.ProdLabel = lighting.ProdLabel;

            if (lighting.HasAdpClass)
                vm.AdpClass = lighting.AdpClass;

            if (lighting.HasPicFacade)
                vm.PicFacade = BusinessContext.Singleton.ImageUrl + lighting.PicFacade;

            LogWindow.AddLog(vm.PicFacade);

            return vm;
        }

        private async void Loading()
        {
            WebServiceProxy proxy = new WebServiceProxy();
            ProdClassArray array = await proxy.SearchProdClass(_parameter.Parameter.ToByteArray());
            var query = from r in array.ProdClassList
                        group r by r.ProdLabel into g
                        select new
                        {
                            Key = g.Key,
                            Value = g
                        };

            List<LableItem> lableItems = new List<LableItem>();
            if (_parameter.Next is NaviTo3Param)
            {
                int j = 0;
                foreach (var item in query)
                {
                    LableItem li = new LableItem();
                    lableItems.Add(li);
                    li.Navigation = Navigation;
                    if (j++ > 0)
                    {
                        li.Margin = new Thickness(0, 20, 0, 0);
                    }
                    li.Title.Text = item.Key;

                    NaviTo3Param naviTo3Param = (NaviTo3Param)_parameter.Next;
                    ObservableCollection<LightingClassModel> items = new ObservableCollection<LightingClassModel>();
                    li.Pictures.ItemsSource = items;
                    foreach (ProdClass child in item.Value)
                    {
                        LightingClassModel model = ConvertForm(child);
                        items.Add(model);

                        CondProdSeries cond = naviTo3Param.Parameter;
                        if (cond.HasLable)
                        {
                            if (cond.Lable != model.ProdLabel)
                                continue;
                        }

                        if (cond.HasAdpaterClass)
                        {
                            if (cond.AdpaterClass != model.AdpClass)
                                continue;
                        }

                        if (cond.HasSeries)
                        {
                            if (cond.Series != model.ProdSeries)
                                continue;
                        }

                        if (cond.HasFacade)
                        {
                            if (cond.Facade != child.PicFacade)
                                continue;
                        }

                        li.Dettach();
                        li.Pictures.SelectedItem = model;
                        li.Attach();
                    }
                }
            }
            else
            {
                int j = 0;
                foreach (var item in query)
                {
                    LableItem li = new LableItem();
                    lableItems.Add(li);
                    li.Navigation = Navigation;
                    if (j++ > 0)
                    {
                        li.Margin = new Thickness(0, 20, 0, 0);
                    }
                    li.Title.Text = item.Key;
                    ObservableCollection<LightingClassModel> items = new ObservableCollection<LightingClassModel>();
                    li.Pictures.ItemsSource = items;
                    foreach (ProdClass child in item.Value)
                    {
                        items.Add(ConvertForm(child));
                    }
                }
            }

            Grid grid = new Grid();
            int i = 0;
            foreach (LableItem item in lableItems)
            {
                RowDefinition row = new RowDefinition();
                row.Height = GridLength.Auto;
                grid.RowDefinitions.Add(row);
                grid.Children.Add(item);
                Grid.SetRow(item, i++);
            }

            LayoutRoot.Children.Add(grid);
            Grid.SetColumn(grid, 1);  
        }

        public PageLevel PageLevel
        {
            get { return PageLevel.Second; }
        }

        public INavigate Navigation
        {
            get;
            set;
        }
    }
}
